package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.internal.util.logging.Log_$logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment){
        int result= paymentService.create(payment);

        if(result>0){
            return new CommonResult<Integer>(200,"支付订单插入成功！serverPort:"+serverPort,result);
        }else{
            return new CommonResult<Integer>(404,"插入失败！",result);
        }
    }

    @GetMapping("/select/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id){

        Payment payment= paymentService.getPayment(id);
        if (payment!=null){
            return new CommonResult<Payment>(200,"成功查询！serverPort:"+serverPort,payment);
        }else{
            return new CommonResult<Payment>( 404,"查询失败:"+id,null);
        }
    }

    @GetMapping("/selectAll")
    public String getAllPayment(){
        List<Payment> list=paymentService.getAllPayment();

        return list.toString();
    }

    @GetMapping(value = "/discovery")
    public Object getDiscovery(){
        //通过客户端获取该注册中心服务器上注册的所有服务名
        List<String> services = discoveryClient.getServices();
        for (String element:services) {
            System.out.println("***********"+element);
        }
        //通过具体的服务名获取该服务上的所有实例，如果是集群的话就有多个实例，如果在之后服务器发生故障的话，就可以通过主机名和端口号进行锁定
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance:instances ) {
            //实例名，主机名，端口号，绝对地址
            System.out.println(instance.getInstanceId()+"  "+instance.getHost()+"  "+instance.getPort()+"  "+instance.getUri());
        }
        return discoveryClient;
    }
    //手写一个负载均衡的轮询算法
    @GetMapping("/lb")
    public String getPaymentLB(){
        return serverPort;
    }
    //设置超时，让feign调用
    @GetMapping("/feign/timeout")
    public String setFeignTimeout() {
        try {
            //模拟程序运行3秒钟，模拟超时任务
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }
}
